"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 备机数据库控制文件权限丢失，备机数据库异常重建
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    步骤 1.启动主备数据库，判断数据库状态；
    步骤 2.修改备机控制文件权限
    步骤 3.kill掉备机进程
    步骤 4.备机重建；
Expect      :    
History     :
    modified: 2021/01/22  modified by @wan005,使用库函数替换脚本中build操作
"""
import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    standby_1_sh = CommonSH('Standby1DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case143.py start------------')
        self.rootNode = Node()
        self.com = Common()
        self.dbUserNode1 = Node(node='PrimaryDbUser')
        self.dbUserNode = Node(node='PrimaryRoot')
        self.dbUserNode2 = Node(node='Standby1Root')
        self.dbUserNode3 = Node(node='Standby1DbUser')
        self.startCmd = f'source {macro.DB_ENV_PATH};gs_om -t start'
        self.statusCmd = f'source {macro.DB_ENV_PATH};gs_om -t status --detail'

    def test_startdb(self):
        # 修改备机控制文件权限
        self.log.info('chmod 000 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')
        self.dbUserNode2.sh('chmod 000 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')
        # ill掉备机进程
        msg1 = self.com.kill_pid(self.dbUserNode2, 9)
        self.assertEqual(msg1, '')
        status = self.dbUserNode1.sh(self.statusCmd).result()
        self.log.info(status)
        self.assertIn('stopped', status)
        # 备机重建；
        result = self.standby_1_sh.buildStandby('')
        self.log.info(result)
        self.assertIn('could not open file', result)

        time.sleep(5)

    def tearDown(self):
        self.dbUserNode2.sh('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control')
        self.dbUserNode1.sh(self.startCmd)
        status = self.dbUserNode1.sh(self.statusCmd).result()
        if not status:
            raise Exception("db status is not true, please check!")
        else:
            pass
        self.log.info(
            '------------------------------------reliability_dbsys143.py finish------------------------------------')
